import { Form, Icon, Input, Button,DatePicker,Select,message} from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;
import HttpUtil from '../../lib/utils/HttpUtil';
import Pubsub from 'pubsub-js';

function hasErrors(fieldsError) {
  return Object.keys(fieldsError).some(field => fieldsError[field]);
}

class SearchForm extends React.Component {
  componentDidMount() {
    this.props.form.validateFields();

    this.pubsub_token = Pubsub.subscribe('search', function (topic,pagination) {
      this.doSearch(pagination);
    }.bind(this));

  }
  componentWillUnmount() {
      Pubsub.unsubscribe(this.pubsub_token);
  }
  doSearch = (pagination) =>{
    this.props.loading(true);

    let values = this.props.form.getFieldsValue();
    if(pagination == null){
      pagination = {
        pageIndex: 1,
        pageSize: 10
      }
    }
    /*
     * 删除无用属性 TODO
     */
    let formData = Object.assign(pagination, {
    <#list columnInfoList as columnInfo>
      <#if columnInfo_has_next>
        ${columnInfo.lowerCamelName}:values.${columnInfo.lowerCamelName},
      <#else>
        ${columnInfo.lowerCamelName}:values.${columnInfo.lowerCamelName}
      </#if>
    </#list>
    });
      HttpUtil.get("/${lowerClassName}/",formData).then((response)=>{

        Pubsub.publish('setData', response);
        this.props.loading(false);
      },(result)=>{
        message.error(result.message);
        this.props.loading(false);
      })
  }
  handleSubmit = (e) => {
    e.preventDefault();
    this.doSearch();
  }

  render() {
    const { getFieldDecorator, getFieldsError, getFieldError, isFieldTouched } = this.props.form;

    const formItemLayout = {
          labelCol: {
            xs: { span: 24 },
            sm: { span: 8 },
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
          },
        };
    return (
      <Form layout="inline" onSubmit={this.handleSubmit}>

      {/*
       * 删除无用列并修改输入框 TODO
       */}
      <#list columnInfoList as columnInfo>

        <FormItem>
          {getFieldDecorator('${columnInfo.lowerCamelName}')(
            <Input style={{ width: 100 }} placeholder="${columnInfo.remark!'占位符###'}" />
          )}
        </FormItem>
      </#list>
        <FormItem>
          <Button size="default" type="primary" htmlType="submit" style={{width: 100}}>搜索</Button>
        </FormItem>
      </Form>
    );
  }
}

const ${className}SearchForm = Form.create()(SearchForm);

export default ${className}SearchForm
